/*     */ package com.hp.bon.sgw.service;
/*     */ 
/*     */ import com.hp.bon.sgw.domain.BusinessRecord;
/*     */ import com.hp.bon.sgw.util.Comutils;
/*     */ import java.io.BufferedInputStream;
/*     */ import java.io.File;
/*     */ import java.io.FileInputStream;
/*     */ import java.io.InputStreamReader;
/*     */ import java.io.LineNumberReader;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Arrays;
/*     */ import java.util.Comparator;
/*     */ import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;
/*     */ import org.springframework.context.support.ClassPathXmlApplicationContext;
/*     */ 
/*     */ public class LoadBusinessRecordService extends Thread
/*     */ {
/*  20 */   private static final Logger logger = LoggerFactory.getLogger(LoadBusinessRecordService.class);
/*     */   private static LoadBusinessRecordService _instance;
/*  23 */   private boolean isSleeping = false;
/*  24 */   private boolean exit = false;
/*  25 */   private int loop_interval = 300;
/*     */   private OuterDBDataService outDataStoreService;
/*     */   private String dataPath;
/*     */   private String backupPath;
/*  52 */   private ClassPathXmlApplicationContext ctx = null;
/*     */ 
/*     */   public LoadBusinessRecordService()
/*     */   {
/*  28 */     setName("LoadBusinessRecordService");
/*     */   }
/*     */ 
/*     */   public static LoadBusinessRecordService getInstance()
/*     */   {
/*  33 */     if (_instance == null) {
/*  34 */       _instance = new LoadBusinessRecordService();
/*     */     }
/*  36 */     return _instance;
/*     */   }
/*     */ 
/*     */   public void exit() {
/*  40 */     this.exit = true;
/*     */   }
/*     */ 
/*     */   public void kickOff() {
/*  44 */     if (this.isSleeping)
/*  45 */       interrupt();
/*     */   }
/*     */ 
/*     */   public void run()
/*     */   {
/*  56 */     logger.info("LoadBusinessRecordService is running ..");
/*  57 */     this.dataPath = System.getProperty("business_data_path");
/*  58 */     this.backupPath = System.getProperty("backupPath");
/*  59 */     this.loop_interval = Integer.valueOf(System.getProperty("loop_interval", "300")).intValue();
/*  60 */     logger.info("business_data_path is " + this.dataPath);
/*  61 */     logger.info("backupPath is " + this.backupPath);
/*  62 */     logger.info("loop_interval is " + this.loop_interval);
/*  63 */     String[] springXmls = { "spring-base.xml" };
/*     */     try {
/*  65 */       this.ctx = new ClassPathXmlApplicationContext(springXmls);
/*  66 */       this.outDataStoreService = ((OuterDBDataService)this.ctx.getBean(OuterDBDataService.class));
/*  67 */       while (!this.exit) {
/*  68 */         doJob();
/*     */         try {
/*  70 */           this.isSleeping = true;
/*  71 */           Thread.sleep(this.loop_interval * 1000);
/*  72 */           this.isSleeping = false;
/*     */         } catch (InterruptedException e) {
/*  74 */           this.isSleeping = false;
/*  75 */           logger.warn("thread interrupted ." + e.fillInStackTrace());
/*     */         }
/*     */       }
/*  78 */       logger.info("exit LoadDataThreader.");
/*     */     } catch (Exception e) {
/*  80 */       logger.warn(e.toString());
/*     */     }
/*     */   }
/*     */ 
/*     */   public void doJob() {
/*  85 */     logger.info(">>>>>> do db save job(sgwdata)");
/*  86 */     File fileDir = new File(this.dataPath);
/*  87 */     if ((fileDir.exists()) && (fileDir.isDirectory()) && (fileDir.listFiles() != null)) {
/*  88 */       File[] files = fileDir.listFiles();
/*     */ 
/*  91 */       Arrays.sort(files, new CompratorByLastModified());
/*     */ 
/*  93 */       for (int i = 0; i < files.length; i++)
/*  94 */         if (canPublishToDB(files[i])) {
/*  95 */           logger.info("process data file " + files[i].getName() + " ......");
/*  96 */           String newFileName = files[i].getAbsolutePath() + ".tmp";
/*  97 */           File newFile = new File(newFileName);
/*  98 */           boolean success = files[i].renameTo(newFile);
/*  99 */           if (success) {
/* 100 */             logger.info("rename " + files[i].getName() + " to " + newFile.getName());
/*     */             try {
/* 102 */               long start = System.currentTimeMillis();
/* 103 */               dataToDB(newFile);
/* 104 */               long used = System.currentTimeMillis() - start;
/* 105 */               if (used >= 300000L)
/* 106 */                 logger.warn("save " + files[i].getName() + " to db end,used time ms=" + used + ",saved too slow.");
/*     */               else {
/* 108 */                 logger.info("save " + files[i].getName() + " to db end,used time ms= " + used);
/*     */               }
/* 110 */               backupFile(newFile, files[i].getName());
/*     */             } catch (Exception e) {
/* 112 */               logger.warn("jsonDataToDB err:" + e);
/*     */             }
/*     */           } else {
/* 115 */             logger.info("rename " + files[i].getName() + " failed.");
/*     */           }
/*     */         }
/*     */     }
/*     */     else {
/* 120 */       logger.info("not found any files under " + this.dataPath);
/*     */     }
/*     */   }
/*     */ 
/*     */   private boolean canPublishToDB(File theFile)
/*     */   {
/* 126 */     String fileName = theFile.getName();
/* 127 */     boolean canProcess = false;
/* 128 */     if ((fileName.endsWith(".sgwdata")) && (theFile.isFile())) {
/*     */       try {
/* 130 */         String curfileTime = Comutils.datafileFormat();
/* 131 */         String fileTime = fileName.substring(fileName.lastIndexOf("_") + 1, fileName.lastIndexOf("."));
/*     */ 
/* 134 */         canProcess = (fileTime.compareToIgnoreCase(curfileTime) < 0) && (theFile.lastModified() < System.currentTimeMillis() - 60000L);
/*     */       } catch (Exception e) {
/* 136 */         logger.warn(e.toString());
/*     */       }
/*     */     }
/* 139 */     return canProcess;
/*     */   }
/*     */ 
/*     */   private void backupFile(File theFile, String fileName) {
/* 143 */     String datePath = Comutils.formatTime(theFile.lastModified(), "yyyyMMdd");
/* 144 */     File destPath = new File(this.backupPath, datePath + File.separator + "record");
/* 145 */     logger.info("prepare backup " + theFile.getName() + "... ");
/* 146 */     if ((!destPath.exists()) && 
/* 147 */       (!destPath.mkdirs())) {
/* 148 */       logger.warn("create file backup dir err:" + destPath.getPath());
/*     */     }
/*     */ 
/* 151 */     boolean suc = theFile.renameTo(new File(destPath, fileName));
/* 152 */     if (!suc) {
/* 153 */       logger.warn("backup file to backup dir err:" + theFile.getName());
/* 154 */       theFile.renameTo(new File(theFile.getParentFile(), theFile.getName() + ".bak"));
/*     */     } else {
/* 156 */       logger.info("backup " + theFile.getName() + " to " + destPath.getAbsolutePath() + File.separator + fileName);
/*     */     }
/* 158 */     logger.info("process end.");
/*     */   }
/*     */ 
/*     */   private void dataToDB(File file) throws Exception {
/* 162 */     LineNumberReader reader = null;
/* 163 */     int savedTotal = 0;
/* 164 */     ArrayList domainList = new ArrayList(1000);
/*     */     try {
/* 166 */       reader = new LineNumberReader(new InputStreamReader(new BufferedInputStream(new FileInputStream(file)), "utf-8"));
/* 167 */       String line = null;
/* 168 */       while ((line = reader.readLine()) != null) {
/* 169 */         BusinessRecord re = BusinessRecord.convertToObject(line);
/* 170 */         if (re != null) {
/* 171 */           domainList.add(re);
/*     */         }
/*     */ 
/* 174 */         if (domainList.size() == 2000) {
/* 175 */           this.outDataStoreService.bulkSaveToDB(domainList);
/* 176 */           savedTotal += domainList.size();
/* 177 */           domainList.clear();
/*     */         }
/*     */       }
/*     */ 
/* 181 */       if (!domainList.isEmpty()) {
/* 182 */         this.outDataStoreService.bulkSaveToDB(domainList);
/* 183 */         savedTotal += domainList.size();
/* 184 */         domainList.clear();
/*     */       }
/* 186 */       domainList = null;
/* 187 */       logger.info("saved " + savedTotal + " records");
/*     */     } catch (Exception e) {
/* 189 */       logger.warn("saved " + savedTotal + " records,exception:" + e);
/*     */     } finally {
/* 191 */       if (reader != null)
/* 192 */         reader.close();
/*     */     }
/*     */   }
/*     */ 
/*     */   static class CompratorByLastModified implements Comparator<File>
/*     */   {
/*     */     public int compare(File f1, File f2) {
/* 199 */       long diff = f1.lastModified() - f2.lastModified();
/* 200 */       if (diff > 0L)
/* 201 */         return 1;
/* 202 */       if (diff == 0L) {
/* 203 */         return 0;
/*     */       }
/* 205 */       return -1;
/*     */     }
/*     */ 
/*     */     public boolean equals(Object obj) {
/* 209 */       return true;
/*     */     }
/*     */   }
/*     */ }

/* Location:           C:\Users\think\Desktop\sgw_2.1.jar
 * Qualified Name:     com.hp.bon.sgw.service.LoadBusinessRecordService
 * JD-Core Version:    0.6.1
 */